package com.project.common;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String encryptPassword(String password) {
        MessageDigest md5 = null;
        String result = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64Encoder = new BASE64Encoder();
            result = base64Encoder.encode(md5.digest(password.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public  static boolean  checkPassword(String inputPwd,String dbPwd){
         String result = encryptPassword(inputPwd);
         if(result.equals(dbPwd)){
             return true;
         }else {
             return false;
         }
    }
}
